package client.ui;

import java.awt.Color;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;

import client.RenrenClient;
import entity.UserInfo;

public class LogIn extends JFrame {

	private static final long serialVersionUID = 7838707806336232285L;

	JLabel user_label;
	JLabel pwd_label;
	JLabel wrong_label;
	JButton loginButton;
	JButton registerButton;

	JFormattedTextField usernamefield;
	JPasswordField pwdfield;

	public LogIn() {
		this.setBounds(560, 250, 240, 200);

		this.setLayout(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle("RenRenSystem");

		user_label = new JLabel("用户名");
		user_label.setBounds(30, 30, 40, 20);
		this.add(user_label);
		usernamefield = new JFormattedTextField();
		usernamefield.setBounds(80, 30, 100, 20);
		usernamefield.setEditable(true);
		this.add(usernamefield);

		pwd_label = new JLabel("密码");
		pwd_label.setBounds(30, 60, 40, 20);
		pwd_label.setVisible(true);
		this.add(pwd_label);
		pwdfield = new JPasswordField();
		pwdfield.setBounds(80, 60, 100, 20);
		pwdfield.addActionListener(new loginListener());
		this.add(pwdfield);

		wrong_label = new JLabel("用户名或密码不正确");
		wrong_label.setBounds(60, 80, 120, 20);
		wrong_label.setForeground(Color.red);
		wrong_label.setVisible(false);
		this.add(wrong_label);

		loginButton = new JButton("登录");
		loginButton.setBounds(40, 110, 60, 30);
		loginButton.addActionListener(new loginListener());
		this.add(loginButton);

		registerButton = new JButton("注册");
		registerButton.setBounds(120, 110, 60, 30);
		registerButton.addActionListener(new registerListener());
		this.add(registerButton);

		this.setVisible(true);
	}

	class loginListener implements ActionListener {
		@SuppressWarnings("deprecation")
		public void actionPerformed(ActionEvent evt) {
			UserInfo usrinfo = null;
			usrinfo = RenrenClient.client.login(usernamefield.getText(),
					pwdfield.getText());
			if (usrinfo != null) {

				wrong_label.setVisible(false);
				RenrenClient.loginFrame.setVisible(false);
				if (RenrenClient.mainFrame == null) {
					RenrenClient.mainFrame = new MainWindow(usrinfo.getUID(),
							usrinfo.getNickname(), usrinfo.getName());
					RenrenClient.mainFrame.setVisible(true);
				}
			} else {
				wrong_label.setVisible(true);
			}
		}
	}

	class registerListener implements ActionListener {
		public void actionPerformed(ActionEvent evt) {
			final JFrame jf = new JFrame("注册新用户");
			jf.setBounds(530, 200, 240, 300);
			jf.setLayout(null);

			JLabel user_label = new JLabel("用户名");
			user_label.setBounds(30, 30, 40, 20);
			jf.add(user_label);
			final JFormattedTextField usernamefield = new JFormattedTextField();
			usernamefield.setBounds(80, 30, 100, 20);
			usernamefield.setEditable(true);
			jf.add(usernamefield);

			JLabel pwd_label = new JLabel("密码");
			pwd_label.setBounds(30, 60, 40, 20);
			pwd_label.setVisible(true);
			jf.add(pwd_label);
			final JPasswordField pwdfield = new JPasswordField();
			pwdfield.setBounds(80, 60, 100, 20);
			jf.add(pwdfield);

			JLabel conf_pwd_label = new JLabel("确认密码");
			conf_pwd_label.setBounds(30, 90, 60, 20);
			conf_pwd_label.setVisible(true);
			jf.add(conf_pwd_label);
			final JPasswordField confpwdfield = new JPasswordField();
			confpwdfield.setBounds(80, 90, 100, 20);
			jf.add(confpwdfield);
			final JLabel wrong_conf_pwd = new JLabel("错误");
			wrong_conf_pwd.setBounds(185, 90, 40, 20);
			wrong_conf_pwd.setVisible(false);
			jf.add(wrong_conf_pwd);

			JLabel nick_label = new JLabel("昵称");
			nick_label.setBounds(30, 120, 40, 20);
			nick_label.setVisible(true);
			jf.add(nick_label);
			final JFormattedTextField nickfield = new JFormattedTextField();
			nickfield.setBounds(80, 120, 100, 20);
			jf.add(nickfield);

			JLabel school_label = new JLabel("学校");
			school_label.setBounds(30, 150, 40, 20);
			school_label.setVisible(true);
			jf.add(school_label);
			final JFormattedTextField schoolfield = new JFormattedTextField();
			schoolfield.setBounds(80, 150, 100, 20);
			jf.add(schoolfield);

			JButton registerButton = new JButton("注册");
			registerButton.setBounds(40, 190, 60, 30);
			registerButton.addActionListener(new loginListener() {
				@Override
				@SuppressWarnings("deprecation")
				public void actionPerformed(ActionEvent evt) {
					if (!pwdfield.getText().equals(confpwdfield.getText())) {
						wrong_conf_pwd.setVisible(true);
					} else {
						wrong_conf_pwd.setVisible(false);
						try {
							if (!RenrenClient.client.register(
									usernamefield.getText(),
									pwdfield.getText(), nickfield.getText(),
									schoolfield.getText())) {
								JOptionPane
										.showMessageDialog(null, "注册失败",
												"提示信息",
												JOptionPane.INFORMATION_MESSAGE);
							} else {
								JOptionPane
										.showMessageDialog(null, "注册成功",
												"提示信息",
												JOptionPane.INFORMATION_MESSAGE);
								jf.setVisible(false);
							}
						} catch (HeadlessException e) {
							e.printStackTrace();
						}
					}
				}
			});
			jf.add(registerButton);

			JButton cancelButton = new JButton("取消");
			cancelButton.setBounds(120, 190, 60, 30);
			cancelButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					jf.setVisible(false);
				}
			});
			jf.add(cancelButton);

			jf.setVisible(true);
		}
	}
}
